package com.apesource.bookBuy.view;

import com.apesource.bookBuy.bean.Book;
import com.apesource.bookBuy.dao.DaoUtil;
import com.apesource.bookBuy.mapper.BookMapper;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @version 1.0
 * @auther:暮雪
 * @date:2025/9/4
 */
@Path("/book")
public class BookController extends Controller {
    public void index() {
        if(getSessionAttr("users")==null){
            redirect("/users/login");
            return;
        }
        SqlSession sqlSession = DaoUtil.getSqlSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        String bookNameKey=getPara("bookNameKey");
        set("keyBookName",bookNameKey);
        List<Book> bookList=bookMapper.getBooksByAuthorAndBookName(null,bookNameKey);
        setAttr("booklist", bookList);
        render("/index.html",0);
    }

    public void addBook() {
        render("/bookAdd.html");

    }

    public void execu_addBook(){
        SqlSession sqlSession= DaoUtil.getSqlSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        String bookName=getPara("bookName");
        String author=getPara("author");
        double price=Double.parseDouble(getPara("bookPrice"));
        int bookNum=getInt("bookNum");
        Book book=new Book();
        book.setBookName(bookName);
        book.setBookNum(bookNum);
        book.setBookAuthor(author);
        book.setBookPrice(price);

        int res=bookMapper.insertBook(book);
        if(res>0){
            sqlSession.commit();
            redirect("/admin");
        }else renderText("添加失败");
        DaoUtil.close(sqlSession);

    }

    public void editBook() {
        if(getSessionAttr("admin")!=null){
        SqlSession sqlSession= DaoUtil.getSqlSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        int bookId=getParaToInt("bookid");
        Book book=bookMapper.getBookById(bookId);
        setAttr("book", book);
        render("/updateBook.html",0);
        DaoUtil.close(sqlSession);}
        else redirect("/admin/login");
    }

    public void execu_editBook(){
        if(getSessionAttr("admin")!=null) {
            SqlSession sqlSession = DaoUtil.getSqlSession();
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            int bookId = getParaToInt("bookId");
            String bookName = getPara("bookName");
            String author = getPara("bookAuthor");
            double price = Double.parseDouble(getPara("bookPrice"));
            int bookNum = getInt("bookNum");
            Book book = new Book();
            book.setBookName(bookName);
            book.setBookNum(bookNum);
            book.setBookAuthor(author);
            book.setBookPrice(price);
            book.setBookId(bookId);

            int res = bookMapper.updateBook(book);
            if (res > 0) {
                sqlSession.commit();
                redirect("/admin");
            } else renderText("修改失败");
            DaoUtil.close(sqlSession);
        }else redirect("/admin/login");
    }

    public void delBook() {
        if(getSessionAttr("admin")!=null){
            SqlSession sqlSession= DaoUtil.getSqlSession();
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            int bookId=getParaToInt("bookid");

            int res=bookMapper.deleteBookById(bookId);
            if (res > 0) {
                sqlSession.commit();
                redirect("/admin");
            }else renderText("删除失败");
            DaoUtil.close(sqlSession);


        }else redirect("/admin/login");
    }
}
